package com.sparkStreaming.demo

import org.apache.spark.SparkConf
import org.apache.spark.streaming.flume.FlumeUtils
import org.apache.spark.streaming.{Seconds, StreamingContext}

/**
  * Spark Streaming整合Flume的第一种方式
  * fixme 官网不建议使用
  */
object _31_FlumePushWordCount {

  def main(args: Array[String]): Unit = {
    val Array(hostname, port) = args

    val sparkConf = new SparkConf().setMaster("local[2]").setAppName("FlumePushWordCount")
    val ssc = new StreamingContext(sparkConf, Seconds(5))

    //使用SparkStreaming整合Flume
    val flumeStream = FlumeUtils.createStream(ssc, "0.0.0.0", 41414)
    //val flumeStream = FlumeUtils.createStream(ssc, hostname, port.toInt)

    flumeStream
      .map(x=> new String(x.event.getBody.array()).trim)
      .flatMap(_.split(" "))
      .map((_,1))
      .reduceByKey(_+_)
      .print()

    ssc.start()
    ssc.awaitTermination()
  }
}
